package org.wu.framework.inner.lazy.orm.solon.config;


import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.wu.framework.inner.lazy.orm.solon.SolonConnectionHandler;
import org.wu.framework.inner.lazy.orm.solon.SolonDataSourceHandler;
import org.wu.framework.lazy.database.datasource.proxy.LazyProxyDataSource;
import org.wu.framework.lazy.database.datasource.proxy.audit.LazyAuditAdapter;
import org.wu.framework.lazy.database.datasource.proxy.handler.ConnectionHandler;
import org.wu.framework.lazy.database.datasource.proxy.handler.DataSourceHandler;
import org.wu.framework.lazy.database.datasource.proxy.handler.JdbcHandlerAdapter;
import org.wu.framework.lazy.database.datasource.proxy.util.LazyDataSourceProxyUtils;

import javax.sql.DataSource;
import java.util.List;

/**
 * description 懒人数据源配置信息
 *
 * @author 吴佳伟
 * @date 2023/05/22 20:39
 */
@Configuration
@Condition(onProperty = "solon.datasource.url")
public class LazyDataSourceProxySolonAutoConfiguration {

    
    @Bean
    public JdbcHandlerAdapter jdbcHandlerAdapter(List<DataSourceHandler> dataSourceHandlerList,
                                                 List<ConnectionHandler> connectionHandlerList,
                                                 LazyAuditAdapter lazyAuditAdapter) {
        return new JdbcHandlerAdapter(dataSourceHandlerList, connectionHandlerList, lazyAuditAdapter);
    }
    // TODO  兼容其他框架使用的数据源代理
    @Bean
    public LazyProxyDataSource lazyProxyDataSource(DataSource dataSource,
                                                   JdbcHandlerAdapter jdbcHandlerAdapter) {
        return LazyDataSourceProxyUtils.proxy(dataSource, jdbcHandlerAdapter);
    }

    /**
     * spring 进行事物管理
     *
     * @return SolonConnectionHandler
     */
    @Bean
    public SolonConnectionHandler solonConnectionHandler() {
        return new SolonConnectionHandler();
    }

    @Bean
    public SolonDataSourceHandler solonDataSourceHandler() {
        return new SolonDataSourceHandler();
    }
}
